<?php

use Monolog\Logger;
use Monolog\Handler\FirePHPHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\BrowserConsoleHandler;

class Log
{
	private static $logsFilePath = ROOT_PATH . "storage/logs/";
	
	private static $mySecurityLogUa = "MySecurityLog18";
	
	
	/**
	 * 获取 Monolog 实例：
	 * $logger = \Log::monolog('mylog', ['Stream','FirePHP']);
	 *
	 * @param string $channel
	 * @param array $handlers
	 * @return Logger
	 * @throws Exception
	 */
	public static function monolog($channel = 'mylog', $handlers = ['Stream','FirePHP'])
	{
		$logger = new Logger($channel);
		
		if(in_array('Stream', $handlers)) {
			$formatter = new LineFormatter("【%datetime%】%level_name%: %message%\n %context%\n\n", "Y-m-d H:i:s");
			$streamHandler = new StreamHandler(static::$logsFilePath.'monologs.log');
			$streamHandler->setFormatter($formatter);
			$logger->pushHandler($streamHandler);
		}
		
		if(in_array('FirePHP', $handlers)) {
			$ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "";
			if (strstr($ua, static::$mySecurityLogUa)) {
				$logger->pushHandler(new FirePHPHandler());
			}
		}
		
		return $logger;
	}
	
	public function test()
	{
		$context = [["name" => "test1"], ["姓名" => "小王"]];
		
		//创建日志频道
		$logger = \Log::monolog('mylog', ['Stream','FirePHP']);
		
//		prin($logger);
		
		//$logger->pushHandler(new \Monolog\Handler\BrowserConsoleHandler());
		
		$logger->info('user list info', $context);
		
		//通过克隆一个之前的实例，改变通道的名字
		$logger2 = $logger->withName('mylog2');
		$logger2->debug('user list debug2', $context);
	}
}